package com.sunfeng.datastructure;

import java.io.*;

/**
 * Java实现稀疏数组
 * 原二维数组--->稀疏数组
 * 将稀疏数组保存到硬盘中
 * 将稀疏数组从硬盘中读出
 * 稀疏数组--->原二维数组
 *
 * @param
 */
public class SparseArray {

    public static void main(String[] args) {
        //创建一个原始的二维数组 11 * 11
        int dataArray1[][] = new int[11][11];
        dataArray1[1][2] = 1;
        dataArray1[2][3] = 2;
        dataArray1[10][10] = 1;

        //打印得到原始二维数组
        for (int i = 0; i < dataArray1.length; i++) {
            for (int j = 0; j < dataArray1[i].length; j++) {
                System.out.printf("%d\t", dataArray1[i][j]);
            }
            System.out.println();
        }


        System.out.println("===============");
        //处理原始数组 获取非零元素个数
        int count = 0;
        for (int i = 0; i < dataArray1.length; i++) {
            for (int j = 0; j < dataArray1[i].length; j++) {
                if (dataArray1[i][j] != 0) {
                    count++;
                }
            }
        }
        //定义稀疏数组
        int sparseArray[][] = new int[count + 1][3];
        sparseArray[0][0] = 11;
        sparseArray[0][1] = 11;
        sparseArray[0][2] = count;

        //构建稀疏数组的值
        int x = 0;
        for (int i = 0; i < dataArray1.length; i++) {
            for (int j = 0; j < dataArray1[i].length; j++) {
                if (dataArray1[i][j] != 0) {
                    x++;
                    sparseArray[x][0] = i;
                    sparseArray[x][1] = j;
                    sparseArray[x][2] = dataArray1[i][j];
                }
            }
        }
        File file = new File("map.data");
        FileOutputStream fos = null;
        OutputStreamWriter write = null;
        try {
            fos = new FileOutputStream(file);
            write = new OutputStreamWriter(fos, "UTF-8");
            //打印稀疏数组
            for (int i = 0; i < sparseArray.length; i++) {
                System.out.printf("%d\t%d\t%d\t\n", sparseArray[i][0], sparseArray[i][1], sparseArray[i][2]);

                if (i == sparseArray.length - 1) {
                    write.append(sparseArray[i][0] + "," + sparseArray[i][1] + "," + sparseArray[i][2]);
                } else {
                    write.append(sparseArray[i][0] + "," + sparseArray[i][1] + "," + sparseArray[i][2] + ",");
                }
            }
            System.out.println("写入文件...");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                write.close();
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


        System.out.println("恢复稀疏数组");
        /**
         * 从文件中读取
         */
        FileInputStream fileInputStream = null;
        InputStreamReader streamReader = null;
        StringBuffer sb = new StringBuffer();
        try {
            fileInputStream = new FileInputStream(file);
            streamReader = new InputStreamReader(fileInputStream, "UTF-8");

            while (streamReader.ready()) {
                sb.append((char) streamReader.read());// 转成char加到StringBuffer对象中
            }

            System.out.println(sb.toString());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {

            try {
                streamReader.close();// 关闭读取流
                fileInputStream.close();// 关闭输入流,释放系统资源
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        String[] str = sb.toString().split(",");

        // 讲文件中的内容转成稀疏数组

        //1. 先读取稀疏数组的第一行， 根据第一行的数据， 创建原始的二维数组


        int dataArrayFile[][] = new int[str.length / 3][3];
        //split.length
        // 给稀疏数组赋值
        int y = 0;
        for (String s : str) {
            dataArrayFile[y / 3][y % 3] = Integer.parseInt(s);
            y++;
        }
        //打印转化后的二维数组
        for (int[] row : dataArrayFile) {
            for (int data : row) {
                System.out.printf("%d\t", data);
            }
            System.out.println();
        }
        System.out.println("********************");

        int dataArray2[][] = new int[sparseArray[0][0]][sparseArray[0][1]];
        //2. 在读取稀疏数组后几行的数据(从第二行开始)， 并赋给 原始的二维数组 即可
        for (int i = 1; i < sparseArray.length; i++) {
            dataArray2[sparseArray[i][0]][sparseArray[i][1]] = sparseArray[i][2];
        }
        //打印转化后的二维数组
        for (int[] row : dataArray2) {
            for (int data : row) {
                System.out.printf("%d\t", data);
            }
            System.out.println();
        }
    }
}
